﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class Combo : FormBase1
    {
        public delegate void DoAction(SVC.client.View_Combo_Lite vcombo);//如果有参数的话，定义参数 

        public DoAction FatherRefresh;
        public Combo()
        {
            InitializeComponent();
            this.Load += new EventHandler(Combo_Load);
        }

        void Combo_Load(object sender, EventArgs e)
        {
            bindtree();
        }

        private void btn_new_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                string ClassCode = treeList1.FocusedNode.GetValue(treeListColumn3).ToString();
                Combo_N frm = new Combo_N(ClassCode);
                frm.FatherRefresh = bind;
                frm.Show();
            }
            catch{ }

        }
        private void bindtree()
        {

            DevExpress.XtraTreeList.Nodes.TreeListNode foucs = treeList1.FocusedNode;
            treeList1.DataSource = my.bw.Combo_Class_Get("ClassCode",my.ACID);
            treeList1.FocusedNode = foucs;
        }

        private void bind(string ClassCode)
        {
            if (!string.IsNullOrEmpty(ClassCode))
            {
                gridControl1.DataSource = my.bw.View_Combo_lite_List(ClassCode, my.ACID);
            }
            
        }

        private void btn_update_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SVC.client.View_Combo_Lite c = gridView1.GetRow(gridView1.FocusedRowHandle) as SVC.client.View_Combo_Lite;
            if (c == null) return;
            Combo_U frm = new Combo_U(c.ComboID);
            frm.FatherRefresh = bind;
            frm.ShowDialog();
        }

        private void btn_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SVC.client.View_Combo_Lite c = gridView1.GetRow(gridView1.FocusedRowHandle) as SVC.client.View_Combo_Lite;
            if (c == null) return;
            string r = my.bw.ComboByComboID_Delete(c.ComboID, my.ACID);
            if (string.IsNullOrEmpty(r))
            {
                bind(c.ClassCode);
            }
            else
            {
                Comm.ShowErorr(r);
            }
        }

        private void btn_sx_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bindtree();

        }

        private void btn_ClassNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ComboClass_N frm = new ComboClass_N();
            frm.FatherRefresh = bindtree;
            frm.ShowDialog();
        }

        private void btn_classUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string ClassCode = treeList1.FocusedNode.GetValue(treeListColumn1).ToString();
            if (!string.IsNullOrEmpty(ClassCode))
            {
                ComboClass_U frm = new ComboClass_U(ClassCode);
                frm.FatherRefresh = bindtree;
                frm.ShowDialog();
            }
        }

        private void btn_classDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            string ClassCode = treeList1.FocusedNode.GetValue(treeListColumn1).ToString();
            if (!string.IsNullOrEmpty(ClassCode))
            {
                if (DialogResult.OK == XtraMessageBox.Show("确定要删除选中的分类","系统提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button2))
                {


                    string r = my.bw.Combo_ClassByClassCode_Delete(ClassCode, my.ACID);
                    if (string.IsNullOrEmpty(r))
                    {
                        bindtree();
                    }
                    else
                    {
                        Comm.ShowErorr(r);
                    }
                }
            }
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            string ClassCode = treeList1.FocusedNode.GetValue(treeListColumn3).ToString();
            if (!string.IsNullOrEmpty(ClassCode))
            {
                gridControl1.DataSource = my.bw.View_Combo_lite_List(ClassCode, my.ACID);
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2 && e.RowHandle >=0)
            {
                SVC.client.View_Combo_Lite vc = gridView1.GetRow(e.RowHandle) as SVC.client.View_Combo_Lite;
                if (FatherRefresh != null)
                {
                    FatherRefresh(vc);
                    this.Close();
                }
            }
        }


    }
}